/****************************************************************************
	This file is part of Basqet.

	Basqet is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Basqet is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
    along with Basqet.  If not, see <http://www.gnu.org/licenses/>.

****************************************************************************/

#include "basketdescriptor.h"

#include "xmlstrings.h"

#include <QDir>
#include <QFileInfo>
#include <QFile>

#include <QtXml>
#include <QtDebug>



BasketDescriptor::BasketDescriptor(const QString& location, const QString& filename)
		:mDescriptorFile(QDir(location), filename),
		mXMLDocument(XML_BASKET_DESCRIPTOR_DOCUMENT)
{
	openDescriptor();
}


void BasketDescriptor::openDescriptor()
{
	QDir		dir = mDescriptorFile.path();

	if (mDescriptorFile.exists())
	{
		qDebug() << "Open the file:" << mDescriptorFile.filePath();
		QFile file(mDescriptorFile.filePath());
		file.open(QIODevice::ReadOnly | QIODevice::Text);

		mXMLDocument.setContent(&file);

		file.close();

		mBasketRoot = mXMLDocument.firstChildElement(XML_BASKET_ROOT);

		mNotePadPageRoot = mBasketRoot.firstChildElement(XML_BASKET_NOTE_PAD_PAGE_OBJECT);

		mNoteListRoot = mBasketRoot.firstChildElement(XML_BASKET_NOTE_LIST);


	}
	else
	{
		qDebug() << "Creating the path: " << dir.path();
		if (!dir.exists())
			dir.mkpath(".");

		createNewDocument();

	}
}


void BasketDescriptor::createNewDocument()
{
	mBasketRoot = mXMLDocument.createElement(XML_BASKET_ROOT);
	mXMLDocument.appendChild(mBasketRoot);

	// Create root for the page-properties.
	mNotePadPageRoot = mXMLDocument.createElement(XML_BASKET_NOTE_PAD_PAGE_OBJECT);
	mBasketRoot.appendChild(mNotePadPageRoot);

	// Create root for notes.
	mNoteListRoot = mXMLDocument.createElement(XML_BASKET_NOTE_LIST);
	mBasketRoot.appendChild(mNoteListRoot);

}

QDomElement BasketDescriptor::createTextNote()
{
	QDomElement tag = mXMLDocument.createElement(XML_NOTE_TEXT_NOTE_OBJECT);
	mNoteListRoot.appendChild(tag);

	return tag;
}



void BasketDescriptor::save()
{
	QString xml = mXMLDocument.toString();

	qDebug() << "Saving to" << mDescriptorFile.filePath();

	QFile	file(mDescriptorFile.filePath());

	file.open(QIODevice::WriteOnly);
	file.write(xml.toUtf8());

	file.close();

}

void BasketDescriptor::printXML()
{

	QString xml = mXMLDocument.toString();
	qDebug() << xml;

}


QDomElement	BasketDescriptor::getNotePadPageRoot()
{
	return mNotePadPageRoot;
}

QDomNodeList BasketDescriptor::getNoteObjects()
{

	return mNoteListRoot.elementsByTagName(XML_NOTE_TEXT_NOTE_OBJECT);
}


QFileInfo BasketDescriptor::getBasketPath()
{
	return QFileInfo(mDescriptorFile.path());
}
